# Scrapy settings for ZhiHuCrawler project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://docs.scrapy.org/en/latest/topics/settings.html
#     https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://docs.scrapy.org/en/latest/topics/spider-middleware.html
import os

filepath = os.path.abspath(os.path.dirname(__file__))

BOT_NAME = 'ZhiHuCrawler'

SPIDER_MODULES = ['ZhiHuCrawler.spiders']
NEWSPIDER_MODULE = 'ZhiHuCrawler.spiders'

MONGO_URI = '127.0.0.1'
MONGO_DB = 'zhihu'

ENV = 'dev'

# UserAgent 用于请求时随机生成
USER_AGENT_PATH = os.path.join(filepath, 'resource', 'useragent.txt')

if ENV == 'dev':
    # 问题爬取深度
    ANSWER_NUM = 1

# 日志级别 - 不打印爬取过程中的参数
if ENV == 'dev':
    LOG_LEVEL = 'DEBUG'
else:
    LOG_LEVEL = 'INFO'

DOWNLOAD_TIMEOUT = 10  # 10s, scrapy默认3分钟

"""
scrapy-redis相关配置
"""

# SCHEDULER = "scrapy_redis.scheduler.Scheduler"
# # REDIS_URL = 'redis://localhost:6379'
# DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"
# # 持久化配置，让待爬取任务队列不会丢失
# SCHEDULER_PERSIST = True

"""
"""

# Ensure use this Scheduler
SCHEDULER = "scrapy_redis_bloomfilter.scheduler.Scheduler"
# Ensure all spiders share same duplicates filter through redis
DUPEFILTER_CLASS = "scrapy_redis_bloomfilter.dupefilter.RFPDupeFilter"
# Redis URL
REDIS_URL = 'redis://localhost:6379'
# Number of Hash Functions to use, defaults to 6
BLOOMFILTER_HASH_NUMBER = 6
# Redis Memory Bit of Bloom Filter Usage, 30 means 2^30 = 128MB, defaults to 30
BLOOMFILTER_BIT = 10
# Persist
SCHEDULER_PERSIST = True

PROXY_URL = 'http://127.0.0.1:8888/proxy/'


# Crawl responsibly by identifying yourself (and your website) on the user-agent
# USER_AGENT = 'ZhiHuCrawler (+http://www.yourdomain.com)'

# Obey robots.txt rules
# ayu：不修改，没得玩
ROBOTSTXT_OBEY = False

# Configure maximum concurrent requests performed by Scrapy (default: 16)
# CONCURRENT_REQUESTS = 32

# Configure a delay for requests for the same website (default: 0)
# See https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
# ayu: 控速，避免影响到网站正常的访问
DOWNLOAD_DELAY = 0.25
# The download delay setting will honor only one of:
# CONCURRENT_REQUESTS_PER_DOMAIN = 16
# CONCURRENT_REQUESTS_PER_IP = 16

# Disable cookies (enabled by default)
# COOKIES_ENABLED = False

# Disable Telnet Console (enabled by default)
# TELNETCONSOLE_ENABLED = False

# Override the default request headers:
DEFAULT_REQUEST_HEADERS = {
    # 'authority': 'www.zhihu.com',
    # 'pragma': 'no-cache',
    # 'cache-control': 'no-cache',
    # 'upgrade-insecure-requests': '1',
    # # 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_16_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36',
    # 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    # 'sec-fetch-site': 'none',
    # 'sec-fetch-mode': 'navigate',
    # 'sec-fetch-user': '?1',
    # 'sec-fetch-dest': 'document',
    # 'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
}

# Enable or disable spider middlewares
# See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
# SPIDER_MIDDLEWARES = {
#    'ZhiHuCrawler.middlewares.ZhihucrawlerSpiderMiddleware': 543,
# }

# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
DOWNLOADER_MIDDLEWARES = {
    'ZhiHuCrawler.middlewares.RandomUserAgentMiddleware': 500,
    'ZhiHuCrawler.middlewares.MyRetryMiddleware': 550,
    'ZhiHuCrawler.middlewares.ProxyMiddleware': 600,
}

# Enable or disable extensions
# See https://docs.scrapy.org/en/latest/topics/extensions.html
# EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
# }

# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
    'ZhiHuCrawler.pipelines.MongoPipeline': 300,
}

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/autothrottle.html
# AUTOTHROTTLE_ENABLED = True
# The initial download delay
# AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
# AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
# AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
# AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
# HTTPCACHE_ENABLED = True
# HTTPCACHE_EXPIRATION_SECS = 0
# HTTPCACHE_DIR = 'httpcache'
# HTTPCACHE_IGNORE_HTTP_CODES = []
# HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'
